﻿/* Copyright 2010 Igor Francé
 * 
 * This file is part of ECMADoc (http://ecmadoc.codeplex.com).
 * 
 * ECMADoc is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * ECMADoc is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with ECMADoc. 
 * If not, see http://www.gnu.org/licenses/.
 */
namespace EcmaDoc.Core.Output
{
	using System;
	using System.Collections.Generic;

	using Cycle99.SourceCodeUtilities.SyntaxHighlighting;

	/// <summary>
	/// Implements the extension object containing various utility functions that can be used in XSLT transformations.
	/// </summary>
	internal class TransformHelper
	{
		/// <summary>
		/// Defines the namespace of this extension object.
		/// </summary>
		public const string Namespace = "http://www.cycle99.com/tools/ecmadoc/transform/help";

		private Dictionary<string, SyntaxHighlighter> formatters;

		private Dictionary<string, SyntaxHighlighter> Formatters
		{
			get
			{
				if (formatters == null)
				{
					formatters = new Dictionary<string, SyntaxHighlighter>();
					formatters.Add("js", new SyntaxHighlighter(new JavaScriptDefinition()));
					formatters.Add("css", new SyntaxHighlighter(new CssDefinition()));
				}

				return formatters;
			}
		}

		/// <summary>
		/// Returns <paramref name="value1"/> if not <c>null</c> or empty, otherwise returns <paramref name="value2"/>.
		/// </summary>
		/// <param name="value1">The desired value.</param>
		/// <param name="value2">The alternative value.</param>
		/// <returns><paramref name="value1"/> if not <c>null</c>, otherwise returns <paramref name="value2"/>.</returns>
		public string IsNull(string value1, string value2)
		{
			return string.IsNullOrEmpty(value1) ? value2 : value1;
		}

		/// <summary>
		/// Evaluates the specified expression, and returns either the <paramref name="trueValue"/> or the 
		/// <paramref name="falseValue"/>, depending on the result.
		/// </summary>
		/// <param name="expression">The expression to evaluate.</param>
		/// <param name="trueValue">The true value.</param>
		/// <param name="falseValue">The false value.</param>
		/// <returns>
		/// The <paramref name="trueValue"/> if <paramref name="expression"/> evaluates to <c>true</c>, or the 
		/// <paramref name="falseValue"/>, if <paramref name="expression"/> evaluates to <c>false</c>.</returns>
		public object Iif(bool expression, object trueValue, object falseValue)
		{
			return expression ? trueValue : falseValue;
		}

		/// <summary>
		/// Converts the specified <paramref name="value"/> to upper case.
		/// </summary>
		/// <param name="value">The value to convert.</param>
		/// <returns>The specified <paramref name="value"/> converted to upper case</returns>
		public string ToUpperCase(string value)
		{
			if (string.IsNullOrEmpty(value))
				return string.Empty;

			return value.ToUpper();
		}

		/// <summary>
		/// Converts the first character of the specified <paramref name="value"/> to upper case.
		/// </summary>
		/// <param name="value">The value to convert.</param>
		/// <returns>The specified <paramref name="value"/> with its first character converted to upper case</returns>
		public string ToUpperCaseFirst(string value)
		{
			if (string.IsNullOrEmpty(value))
				return string.Empty;

			return value.Substring(0, 1).ToUpper() + value.Substring(1);
		}

		/// <summary>
		/// Converts the specified <paramref name="value"/> to lower case.
		/// </summary>
		/// <param name="value">The value to convert.</param>
		/// <returns>The specified <paramref name="value"/> converted to lower case</returns>
		public string ToLowerCase(string value)
		{
			if (string.IsNullOrEmpty(value))
				return string.Empty;

			return value.ToLower();
		}

		/// <summary>
		/// Syntax highlights the specified <paramref name="value"/> using the specified language to interpret it.
		/// </summary>
		/// <param name="value">The value to syntax highlight</param>
		/// <param name="language">The programming language of the specified <paramref name="value"/>.</param>
		/// <returns>An HTML string that surrounds the language elements with css class names necessary to distinguish
		/// them for syntax highlighting.</returns>
		public string ColorCode(string value, string language)
		{
			if (language == "css")
				return Formatters["css"].Format(value);

			return Formatters["js"].Format(value);
		}

		/// <summary>
		/// Syntax highlights the specified <paramref name="value"/> interpreting it as JavaScript.
		/// </summary>
		/// <param name="value">The value to syntax highlight</param>
		/// <returns>An HTML string that surrounds the JavaScript language elements with css class names necessary to 
		/// distinguish them for syntax highlighting.</returns>
		public string ColorCodeJavaScript(string value)
		{
			return Formatters["js"].Format(value);
		}
	}
}
